package com.ruoyi.common.utils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class RSAUtil {

    public static String getSign(Map<String, String> params, String key) {
        try {
//            key="MIICXAIBAAKBgQDY0RJX7m2ySh5y3jFzegNCXhJiVYuKFSBSdbtt7DTI7uUeHgp4hxkZR2cQ63L6wCoksdc5gQ2lsBrp5IALjhCT0wCb9YWDkM/5S2xCJHJTtlEPHYy7SE7xNszqUJosas3exROZGOMvc3koVT6oBzljIKyyl7EaQXNKeOny4uO63QIDAQABAoGAcr36YjSZMyCMiqfSLdWlger8+bNh/Hp/t4P85Ah8StY6xLyLOAvvGXSzM74XBGLMKs0/JHyKt2PCaoN1zCer10X45ekebtu+bChZx9fLIcLZgoJseUftcjg78fYHe7MOpvZu2fGQY7eBNZ/K/tcRfGPCNKI1vZGs4jqm1rW4yrUCQQDy/CqadVzYoB2rEuootqYgtKbui3PxdCWSejX3PLNG7OG2SDoqp9DfqSyUkjP4GtmwRQtJOWRydS828DAumvOHAkEA5G4VHvLVYqrBLI3dznb8L7Cx77oEFjZyK4tWqRXklzxu6yjfTsKNi1qsjZHH14WrjYqOWQrCQ82bEMej1Qm/ewJBAJ+tz2GJ1uYU8upFl9QBk9LSZRFGW39FUJud7HD5Vuc5LGtZs7/sYHta9Yz9N2HIY15PQUdxi+0bzVdRKJi4A2kCQGJI0yGbziBD5BXtfHAnVd6XIhhJaxS7nGgL7d+WoeJIiFj6HwrePtSk0PWgseGTi/q8kP7S2D8gKgYQP/eb2B0CQGizqvjxL9FeSKUZ3jRRRSXU7pJDQzlzwxIsul0fpaHGlDw/k+p8SIMr/owZ9HwECzix67+UIdKzcKh8G6mQBJ0=";
//            key="MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBANjRElfubbJKHnLeMXN6A0JeEmJVi4oVIFJ1u23sNMju5R4eCniHGRlHZxDrcvrAKiSx1zmBDaWwGunkgAuOEJPTAJv1hYOQz/lLbEIkclO2UQ8djLtITvE2zOpQmixqzd7FE5kY4y9zeShVPqgHOWMgrLKXsRpBc0p46fLi47rdAgMBAAECgYByvfpiNJkzIIyKp9It1aWB6vz5s2H8en+3g/zkCHxK1jrEvIs4C+8ZdLMzvhcEYswqzT8kfIq3Y8Jqg3XMJ6vXRfjl6R5u275sKFnH18shwtmCgmx5R+1yODvx9gd7sw6m9m7Z8ZBjt4E1n8r+1xF8Y8I0ojW9kaziOqbWtbjKtQJBAPL8Kpp1XNigHasS6ii2piC0pu6Lc/F0JZJ6Nfc8s0bs4bZIOiqn0N+pLJSSM/ga2bBFC0k5ZHJ1LzbwMC6a84cCQQDkbhUe8tViqsEsjd3OdvwvsLHvugQWNnIri1apFeSXPG7rKN9Owo2LWqyNkcfXhauNio5ZCsJDzZsQx6PVCb97AkEAn63PYYnW5hTy6kWX1AGT0tJlEUZbf0VQm53scPlW5zksa1mzv+xge1r1jP03YchjXk9BR3GL7RvNV1EomLgDaQJAYkjTIZvOIEPkFe18cCdV3pciGElrFLucaAvt35ah4kiIWPofCt4+1KTQ9aCx4ZOL+ryQ/tLYPyAqBhA/95vYHQJAaLOq+PEv0V5IpRneNFFFJdTukkNDOXPDEiy6XR+locaUPD+T6nxIgyv+jBn0fAQLOLHrv5Qh0rNwqHwbqZAEnQ==";
            //PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(key));
            PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(new BASE64Decoder().decodeBuffer(key));

            KeyFactory keyf = KeyFactory.getInstance("RSA");
            PrivateKey privateKey = keyf.generatePrivate(priPKCS8);
            Signature sign = Signature.getInstance("SHA1withRSA");
            sign.initSign(privateKey);
            String Paramstr = createLinkString(params);
            byte[] data = Paramstr.getBytes();
            //更新用于签名的数据
            sign.update(data);
            byte[] signature = sign.sign();
            //return Base64.getEncoder().encodeToString(signature);
            return new BASE64Encoder().encode(signature);

        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String createLinkString(Map<String, String> params) {

        ArrayList<String> keys = new ArrayList<String>(params.keySet());
        Collections.sort(keys);
        String prestr = "";
        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = params.get(key);
            if (i == keys.size() - 1) {// 拼接时，不包括最后一个&字符
                prestr = prestr + key + "=" + value;
            } else {
                prestr = prestr + key + "=" + value + "&";
            }
        }
        return prestr;

    }

    public static void main(String[] args) {
        Map<String, String> params =  new HashMap<>();
        params.put("key", "value");
        getSign(params, "");
    }
}